<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-fortify" tabindex="-1"><a class="header-anchor" href="#laravel-fortify"><span>Laravel Fortify</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#what-is-fortify">什么是 Fortify？</a></li>
<li><a href="#when-should-i-use-fortify">何时应该使用 Fortify？</a></li>
</ul>
</li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#fortify-features">Fortify 功能</a></li>
<li><a href="#disabling-views">禁用视图</a></li>
</ul>
</li>
<li><a href="#authentication">身份验证</a>
<ul>
<li><a href="#customizing-user-authentication">自定义用户身份验证</a></li>
<li><a href="#customizing-the-authentication-pipeline">自定义身份验证管道</a></li>
<li><a href="#customizing-authentication-redirects">自定义重定向</a></li>
</ul>
</li>
<li><a href="#two-factor-authentication">双因素身份验证</a>
<ul>
<li><a href="#enabling-two-factor-authentication">启用双因素身份验证</a></li>
<li><a href="#authenticating-with-two-factor-authentication">使用双因素身份验证进行身份验证</a></li>
<li><a href="#disabling-two-factor-authentication">禁用双因素身份验证</a></li>
</ul>
</li>
<li><a href="#registration">注册</a>
<ul>
<li><a href="#customizing-registration">自定义注册</a></li>
</ul>
</li>
<li><a href="#password-reset">重置密码</a>
<ul>
<li><a href="#requesting-a-password-reset-link">请求重置密码链接</a></li>
<li><a href="#resetting-the-password">重置密码</a></li>
<li><a href="#customizing-password-resets">自定义密码重置</a></li>
</ul>
</li>
<li><a href="#email-verification">电子邮件验证</a>
<ul>
<li><a href="#protecting-routes">保护路由</a></li>
</ul>
</li>
<li><a href="#password-confirmation">密码确认</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/fortify" target="_blank" rel="noopener noreferrer">Laravel Fortify</a> 是 Laravel 的一个与前端无关的身份验证后端实现。Fortify 注册了实现所有 Laravel 身份验证功能所需的路由和控制器，包括登录、注册、重置密码、电子邮件验证等。安装 Fortify 后，你可以运行 <code v-pre>route:list</code> Artisan 命令查看 Fortify 注册的路由。</p>
<p>由于 Fortify 不提供自己的用户界面，它旨在与你自己的用户界面配对，该界面向注册的路由发出请求。我们将在本文档的其余部分讨论如何向这些路由发出请求。</p>
<blockquote>
<p><strong>注意</strong><br>
请记住，Fortify 是一个旨在帮助你快速实现 Laravel 身份验证功能的包。<strong>你不必使用它。</strong> 你始终可以按照<a href="https://learnku.com/docs/laravel/11.x/authentication" target="_blank" rel="noopener noreferrer">身份验证</a>、<a href="https://learnku.com/docs/laravel/11.x/passwords" target="_blank" rel="noopener noreferrer">密码重置</a>和<a href="https://learnku.com/docs/laravel/11.x/verification" target="_blank" rel="noopener noreferrer">电子邮件验证</a>文档中提供的文档手动与 Laravel 的身份验证服务交互。</p>
</blockquote>
<h3 id="什么是-fortify" tabindex="-1"><a class="header-anchor" href="#什么是-fortify"><span>什么是 Fortify？</span></a></h3>
<p>正如前面提到的，Laravel Fortify 是 Laravel 的一个与前端无关的身份验证后端实现。Fortify 注册了实现所有 Laravel 身份验证功能所需的路由和控制器，包括登录、注册、重置密码、电子邮件验证等。</p>
<p><strong>你不必使用 Fortify 来使用 Laravel 的身份验证功能。</strong> 你始终可以按照<a href="https://learnku.com/docs/laravel/11.x/authentication" target="_blank" rel="noopener noreferrer">身份验证</a>、<a href="https://learnku.com/docs/laravel/11.x/passwords" target="_blank" rel="noopener noreferrer">密码重置</a>和<a href="https://learnku.com/docs/laravel/11.x/verification" target="_blank" rel="noopener noreferrer">电子邮件验证</a>文档中提供的文档手动与 Laravel 的身份验证服务交互。</p>
<p>如果你是 Laravel 的新手，你可能希望在尝试使用 Laravel Fortify 之前先探索<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">Laravel Breeze</a>应用程序起始套件。Laravel Breeze 为你的应用程序提供了一个身份验证脚手架，包括一个使用<a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS</a>构建的用户界面。与 Fortify 不同，Breeze 将其路由和控制器直接发布到你的应用程序中。这使你可以在允许 Laravel Fortify 为你实现这些功能之前，研究和熟悉 Laravel 的身份验证功能。</p>
<p>Laravel Fortify 本质上将 Laravel Breeze 的路由和控制器作为一个不包含用户界面的包提供。这使你仍然可以快速搭建应用程序身份验证层的后端实现，而不受任何特定前端观点的约束。</p>
<h3 id="何时应该使用-fortify" tabindex="-1"><a class="header-anchor" href="#何时应该使用-fortify"><span>何时应该使用 Fortify？</span></a></h3>
<p>你可能想知道何时适合使用 Laravel Fortify。首先，如果你正在使用 Laravel 的<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">应用程序起始套件</a>，你无需安装 Laravel Fortify，因为所有 Laravel 的应用程序起始套件已经提供了完整的身份验证实现。</p>
<p>如果你没有使用应用程序起始套件并且你的应用程序需要身份验证功能，你有两个选择：手动实现应用程序的身份验证功能或者使用 Laravel Fortify 提供这些功能的后端实现。</p>
<p>如果你选择安装 Fortify，你的用户界面将向 Fortify 的身份验证路由发出请求，这些路由在本文档中有详细说明，用于对用户进行身份验证和注册。</p>
<p>如果你选择手动与 Laravel 的身份验证服务交互而不是使用 Fortify，你可以按照<a href="https://learnku.com/docs/laravel/11.x/authentication" target="_blank" rel="noopener noreferrer">身份验证</a>、<a href="https://learnku.com/docs/laravel/11.x/passwords" target="_blank" rel="noopener noreferrer">密码重置</a>和<a href="https://learnku.com/docs/laravel/11.x/verification" target="_blank" rel="noopener noreferrer">电子邮件验证</a>文档中提供的文档进行操作。</p>
<h4 id="laravel-fortify-和-laravel-sanctum" tabindex="-1"><a class="header-anchor" href="#laravel-fortify-和-laravel-sanctum"><span>Laravel Fortify 和 Laravel Sanctum</span></a></h4>
<p>一些开发人员对<a href="https://learnku.com/docs/laravel/11.x/sanctum" target="_blank" rel="noopener noreferrer">Laravel Sanctum</a>和 Laravel Fortify 之间的区别感到困惑。因为这两个包解决了两个不同但相关的问题，Laravel Fortify 和 Laravel Sanctum 不是互斥或竞争的包。</p>
<p>Laravel Sanctum 仅关注管理 API 令牌和使用会话 cookie 或令牌对现有用户进行身份验证。Sanctum 不提供处理用户注册、密码重置等功能的路由。</p>
<p>如果你正在尝试手动构建一个应用程序的身份验证层，该应用程序提供 API 或用作单页面应用程序的后端，那么完全可以同时使用 Laravel Fortify（用于用户注册、密码重置等）和 Laravel Sanctum（API 令牌管理、会话身份验证）。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>要开始使用，使用 Composer 包管理器安装 Fortify：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/fortify</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，使用 <code v-pre>fortify:install</code> Artisan 命令发布 Fortify 的资源：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan fortify:install</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>该命令将发布 Fortify 的操作到你的 <code v-pre>app/Actions</code> 目录中，如果该目录不存在则会创建。此外，<code v-pre>FortifyServiceProvider</code>、配置文件和所有必要的数据库迁移也将被发布。</p>
<p>接下来，你应该迁移数据库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="fortify-功能" tabindex="-1"><a class="header-anchor" href="#fortify-功能"><span>Fortify 功能</span></a></h3>
<p><code v-pre>fortify</code> 配置文件包含一个 <code v-pre>features</code> 配置数组。该数组定义了 Fortify 默认会暴露的后端路由/功能。如果你没有将 Fortify 与 <a href="https://jetstream.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Jetstream</a> 结合使用，我们建议你只启用以下功能，这些功能是大多数 Laravel 应用程序提供的基本身份验证功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'features'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">registration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">resetPasswords</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">emailVerification</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="禁用视图" tabindex="-1"><a class="header-anchor" href="#禁用视图"><span>禁用视图</span></a></h3>
<p>默认情况下，Fortify 定义了用于返回视图的路由，比如登录界面或注册界面。但是，如果你正在构建一个由 JavaScript 驱动的单页面应用程序，你可能不需要这些路由。因此，你可以通过在你的应用程序的 <code v-pre>config/fortify.php</code> 配置文件中将 <code v-pre>views</code> 配置值设置为 <code v-pre>false</code> 来完全禁用这些路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'views'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="禁用视图和密码重置" tabindex="-1"><a class="header-anchor" href="#禁用视图和密码重置"><span>禁用视图和密码重置</span></a></h4>
<p>如果你选择禁用 Fortify 的视图，并且你将为你的应用程序实现密码重置功能，你仍然应该定义一个名为 <code v-pre>password.reset</code> 的路由，该路由负责显示你的应用程序的“重置密码”视图。这是必要的，因为 Laravel 的 <code v-pre>Illuminate\Auth\Notifications\ResetPassword</code> 通知将通过 <code v-pre>password.reset</code> 命名路由生成密码重置 URL。</p>
<h2 id="身份验证" tabindex="-1"><a class="header-anchor" href="#身份验证"><span>身份验证</span></a></h2>
<p>首先，我们需要指示 Fortify 如何返回我们的“登录”视图。请记住，Fortify 是一个无头身份验证库。如果你想要一个已经为你完成的 Laravel 身份验证功能的前端实现，你应该使用一个<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">应用程序起始套件</a>。</p>
<p>所有身份验证视图的渲染逻辑可以使用 <code v-pre>Laravel\Fortify\Fortify</code> 类中提供的适当方法进行自定义。通常情况下，你应该在你的应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用这个方法。Fortify 将负责定义返回此视图的 <code v-pre>/login</code> 路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">loginView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你的登录模板应包含一个提交 POST 请求到 <code v-pre>/login</code> 的表单。<code v-pre>/login</code> 端点期望一个字符串 <code v-pre>email</code> / <code v-pre>username</code> 和一个 <code v-pre>password</code>。邮箱 / 用户名字段的名称应与 <code v-pre>config/fortify.php</code> 配置文件中的 <code v-pre>username</code> 值匹配。此外，可以提供一个布尔型 <code v-pre>remember</code> 字段，以指示用户是否希望使用 Laravel 提供的“记住我”功能。</p>
<p>如果登录尝试成功，Fortify 将重定向你到通过应用程序的 <code v-pre>fortify</code> 配置文件中的 <code v-pre>home</code> 配置选项配置的 URI。如果登录请求是 XHR 请求，将返回一个 200 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回登录界面，并且验证错误将通过共享的 <code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/11.x/validation#quick-displaying-the-validation-errors" target="_blank" rel="noopener noreferrer">Blade 模板变量</a>提供给你。或者，在 XHR 请求的情况下，验证错误将在 422 HTTP 响应中返回。</p>
<h3 id="自定义用户认证" tabindex="-1"><a class="header-anchor" href="#自定义用户认证"><span>自定义用户认证</span></a></h3>
<p>Fortify 将根据提供的凭据和为你的应用程序配置的身份验证守卫自动检索和验证用户。然而，有时你可能希望完全定制登录凭据的验证方式和用户的检索方式。幸运的是，Fortify 允许你使用 <code v-pre>Fortify::authenticateUsing</code> 方法轻松实现这一点。</p>
<p>这个方法接受一个闭包，该闭包接收传入的 HTTP 请求。该闭包负责验证附加到请求的登录凭据并返回相应的用户实例。如果凭据无效或找不到用户，则闭包应返回 <code v-pre>null</code> 或 <code v-pre>false</code>。通常情况下，这个方法应该从你的 <code v-pre>FortifyServiceProvider</code> 的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Hash</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Bootstrap any application services.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">authenticateUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">            <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">password</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">password</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="身份验证守卫" tabindex="-1"><a class="header-anchor" href="#身份验证守卫"><span>身份验证守卫</span></a></h4>
<p>你可以在应用程序的 <code v-pre>fortify</code> 配置文件中自定义 Fortify 使用的身份验证守卫。但是，你应确保配置的守卫是 <code v-pre>Illuminate\Contracts\Auth\StatefulGuard</code> 的实现。如果你尝试使用 Laravel Fortify 来验证单页面应用程序（SPA），你应该结合使用 Laravel 的默认 <code v-pre>web</code> 守卫和 <a href="https://laravel.com/docs/sanctum" target="_blank" rel="noopener noreferrer">Laravel Sanctum</a>。</p>
<h3 id="自定义身份验证流程" tabindex="-1"><a class="header-anchor" href="#自定义身份验证流程"><span>自定义身份验证流程</span></a></h3>
<p>Laravel Fortify 通过一系列可调用类来验证登录请求。如果你愿意，你可以定义一个自定义的类流程，用于处理登录请求。每个类应该有一个接收传入的 <code v-pre>Illuminate\Http\Request</code> 实例的 <code v-pre>__invoke</code> 方法，并且像 <a href="https://learnku.com/docs/laravel/11.x/middleware" target="_blank" rel="noopener noreferrer">中间件</a> 一样，一个 <code v-pre>$next</code> 变量，用于调用以将请求传递给管道中的下一个类。</p>
<p>要定义你的自定义流程，你可以使用 <code v-pre>Fortify::authenticateThrough</code> 方法。这个方法接受一个闭包，该闭包应返回一个类数组，用于处理登录请求。通常情况下，这个方法应该从你的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用。</p>
<p>下面的示例包含了默认的流程定义，你可以将其用作制作自定义修改的起点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>AttemptToAuthenticate</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>EnsureLoginIsNotThrottled</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>PrepareAuthenticatedSession</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>RedirectIfTwoFactorAuthenticatable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">authenticateThrough</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">array_filter</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fortify.limiters.login'</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token constant">null</span> <span class="token punctuation">:</span> <span class="token class-name static-context">EnsureLoginIsNotThrottled</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">enabled</span><span class="token punctuation">(</span><span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">twoFactorAuthentication</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token class-name static-context">RedirectIfTwoFactorAuthenticatable</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">AttemptToAuthenticate</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">PrepareAuthenticatedSession</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="身份验证节流" tabindex="-1"><a class="header-anchor" href="#身份验证节流"><span>身份验证节流</span></a></h4>
<p>默认情况下，Fortify 将使用 <code v-pre>EnsureLoginIsNotThrottled</code> 中间件来节流身份验证尝试。这个中间件会对与用户名和 IP 地址组合唯一的尝试进行节流。</p>
<p>一些应用程序可能需要不同的身份验证尝试节流方法，比如仅按 IP 地址节流。因此，Fortify 允许你通过 <code v-pre>fortify.limiters.login</code> 配置选项指定你自己的<a href="https://learnku.com/docs/laravel/11.x/routing#rate-limiting" target="_blank" rel="noopener noreferrer">速率限制器</a>。当然，这个配置选项位于你的应用程序的 <code v-pre>config/fortify.php</code> 配置文件中。</p>
<blockquote>
<p><strong>注意</strong><br>
结合使用节流、<a href="https://learnku.com/docs/laravel/11.x/fortify#two-factor-authentication" target="_blank" rel="noopener noreferrer">双因素身份验证</a>和外部网络应用程序防火墙（WAF）将为你的合法应用程序用户提供最强大的防御。</p>
</blockquote>
<h3 id="自定义重定向" tabindex="-1"><a class="header-anchor" href="#自定义重定向"><span>自定义重定向</span></a></h3>
<p>如果登录尝试成功，Fortify 将重定向你到通过应用程序的 <code v-pre>fortify</code> 配置文件中的 <code v-pre>home</code> 配置选项配置的 URI。如果登录请求是 XHR 请求，将返回一个 200 HTTP 响应。用户登出应用程序后，用户将被重定向到 <code v-pre>/</code> URI。</p>
<p>如果你需要对这个行为进行高级定制，你可以将 <code v-pre>LoginResponse</code> 和 <code v-pre>LogoutResponse</code> 接口的实现绑定到 Laravel 的<a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a>中。通常情况下，这应该在你的应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>register</code> 方法中完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>LogoutResponse</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 注册任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">instance</span><span class="token punctuation">(</span><span class="token class-name static-context">LogoutResponse</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">implements</span> <span class="token class-name">LogoutResponse</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toResponse</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="双因素身份验证" tabindex="-1"><a class="header-anchor" href="#双因素身份验证"><span>双因素身份验证</span></a></h2>
<p>当启用 Fortify 的双因素身份验证功能时，用户在身份验证过程中需要输入一个六位数字令牌。这个令牌是使用基于时间的一次性密码（TOTP）生成的，可以从任何支持 TOTP 的移动身份验证应用程序（如 Google Authenticator）中获取。</p>
<p>在开始之前，你应该首先确保你的应用程序的 <code v-pre>App\Models\User</code> 模型使用了 <code v-pre>Laravel\Fortify\TwoFactorAuthenticatable</code> 特性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>TwoFactorAuthenticatable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">,</span> TwoFactorAuthenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你应该在你的应用程序中构建一个屏幕，让用户可以管理他们的双因素身份验证设置。这个屏幕应该允许用户启用和禁用双因素身份验证，以及重新生成他们的双因素身份验证恢复码。</p>
<blockquote>
<p>默认情况下，<code v-pre>fortify</code> 配置文件中的 <code v-pre>features</code> 数组指示 Fortify 的双因素身份验证设置在修改前需要密码确认。因此，在继续之前，你的应用程序应该实现 Fortify 的<a href="#password-confirmation">密码确认</a>功能。</p>
</blockquote>
<h3 id="启用双因素身份验证" tabindex="-1"><a class="header-anchor" href="#启用双因素身份验证"><span>启用双因素身份验证</span></a></h3>
<p>要开始启用双因素身份验证，你的应用程序应该向 Fortify 定义的 <code v-pre>/user/two-factor-authentication</code> 端点发起 POST 请求。如果请求成功，用户将被重定向回先前的 URL，并且 <code v-pre>status</code> 会话变量将被设置为 <code v-pre>two-factor-authentication-enabled</code>。你可以在模板中检测这个 <code v-pre>status</code> 会话变量，以显示适当的成功消息。如果请求是 XHR 请求，将返回 <code v-pre>200</code> HTTP 响应。</p>
<p>在选择启用双因素身份验证后，用户仍需要通过提供有效的双因素身份验证代码来“确认”他们的双因素身份验证配置。因此，你的“成功”消息应该指示用户仍然需要双因素身份验证确认：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line">@if (session('status') == 'two-factor-authentication-enabled')</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mb-4 font-medium text-sm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        请在下面完成双因素身份验证配置。</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你应该显示双因素身份验证的 QR 码，以便用户可以扫描到他们的认证器应用程序中。如果你在 Blade 中渲染你的应用程序的前端，你可以使用用户实例上可用的 <code v-pre>twoFactorQrCodeSvg</code> 方法来检索 QR 码的 SVG：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">twoFactorQrCodeSvg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你正在构建一个由 JavaScript 驱动的前端，你可以向 <code v-pre>/user/two-factor-qr-code</code> 端点发起 XHR GET 请求，以检索用户的双因素身份验证 QR 码。这个端点将返回一个包含 <code v-pre>svg</code> 键的 JSON 对象。</p>
<h4 id="确认双因素身份验证" tabindex="-1"><a class="header-anchor" href="#确认双因素身份验证"><span>确认双因素身份验证</span></a></h4>
<p>除了显示用户的双因素身份验证 QR 码之外，你还应该提供一个文本输入框，用户可以在其中提供有效的身份验证代码来“确认”他们的双因素身份验证配置。这个代码应该通过向 Fortify 定义的 <code v-pre>/user/confirmed-two-factor-authentication</code> 端点发起 POST 请求提供给 Laravel 应用程序。</p>
<p>如果请求成功，用户将被重定向回先前的 URL，并且 <code v-pre>status</code> 会话变量将被设置为 <code v-pre>two-factor-authentication-confirmed</code>：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line">@if (session('status') == 'two-factor-authentication-confirmed')</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mb-4 font-medium text-sm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        双因素身份验证已成功确认和启用。</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果通过 XHR 请求向双因素身份验证确认端点发起请求，将返回 <code v-pre>200</code> HTTP 响应。</p>
<h4 id="显示恢复码" tabindex="-1"><a class="header-anchor" href="#显示恢复码"><span>显示恢复码</span></a></h4>
<p>你还应该显示用户的双因素身份验证恢复码。这些恢复码允许用户在丢失移动设备访问权限时进行身份验证。如果你在 Blade 中渲染你的应用程序的前端，你可以通过认证用户实例访问恢复码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">(</span><span class="token keyword type-casting">array</span><span class="token punctuation">)</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">recoveryCodes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你正在构建一个由 JavaScript 驱动的前端，你可以向 <code v-pre>/user/two-factor-recovery-codes</code> 端点发起 XHR GET 请求。这个端点将返回一个包含用户恢复码的 JSON 数组。</p>
<p>要重新生成用户的恢复码，你的应用程序应该向 <code v-pre>/user/two-factor-recovery-codes</code> 端点发起 POST 请求。</p>
<h3 id="使用双因素身份验证进行认证" tabindex="-1"><a class="header-anchor" href="#使用双因素身份验证进行认证"><span>使用双因素身份验证进行认证</span></a></h3>
<p>在身份验证过程中，Fortify 将自动将用户重定向到你的应用程序的双因素身份验证挑战屏幕。然而，如果你的应用程序正在进行 XHR 登录请求，成功认证尝试后返回的 JSON 响应将包含一个具有 <code v-pre>two_factor</code> 布尔属性的 JSON 对象。你应该检查这个值，以确定是否应该重定向到你的应用程序的双因素身份验证挑战屏幕。</p>
<p>要开始实现双因素身份验证功能，我们需要指示 Fortify 如何返回我们的双因素身份验证挑战视图。所有 Fortify 的身份验证视图渲染逻辑都可以使用 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常情况下，你应该从你的应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">twoFactorChallengeView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.two-factor-challenge'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 会负责定义返回此视图的<code v-pre>/two-factor-challenge</code>路由。你的<code v-pre>two-factor-challenge</code>模板应包含一个表单，该表单会向<code v-pre>/two-factor-challenge</code>端点发起POST请求。<code v-pre>/two-factor-challenge</code>操作需要一个包含有效 TOTP 令牌的<code v-pre>code</code>字段，或者一个包含用户恢复码之一的<code v-pre>recovery_code</code>字段。</p>
<p>如果登录尝试成功，Fortify将重定向用户到通过你的应用程序<code v-pre>fortify</code>配置文件中的<code v-pre>home</code>配置选项配置的URI。如果登录请求是一个XHR请求，则将返回一个204 HTTP响应。</p>
<p>如果请求不成功，用户将被重定向回双因素身份验证挑战屏幕，并且验证错误将通过共享的<code v-pre>$errors</code> Blade 模板变量提供给你。或者，在XHR请求的情况下，验证错误将在422 HTTP响应中返回。</p>
<h3 id="禁用双因素身份验证" tabindex="-1"><a class="header-anchor" href="#禁用双因素身份验证"><span>禁用双因素身份验证</span></a></h3>
<p>要禁用双因素身份验证，你的应用程序应该向<code v-pre>/user/two-factor-authentication</code>端点发起DELETE请求。请记住，Fortify的双因素身份验证端点在调用之前需要<a href="#password-confirmation">密码确认</a>。</p>
<h2 id="注册" tabindex="-1"><a class="header-anchor" href="#注册"><span>注册</span></a></h2>
<p>要开始实现我们应用程序的注册功能，我们需要指示Fortify如何返回我们的“register”视图。请记併，Fortify是一个无头身份验证库。如果你想要一个已经为你完成的Laravel身份验证功能的前端实现，你应该使用一个<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">应用程序起始工具包</a>。</p>
<p>所有Fortify的视图渲染逻辑都可以使用<code v-pre>Laravel\Fortify\Fortify</code>类提供的适当方法进行自定义。通常情况下，你应该从你的<code v-pre>App\Providers\FortifyServiceProvider</code>类的<code v-pre>boot</code>方法中调用这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">registerView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.register'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify会负责定义返回此视图的 <code v-pre>/register</code> 路由。你的 <code v-pre>register</code> 模板应包含一个表单，该表单会向Fortify定义的 <code v-pre>/register</code> 端点发起POST请求。</p>
<p><code v-pre>/register</code> 端点需要一个字符串 <code v-pre>name</code> 、字符串电子邮件地址/用户名、<code v-pre>password</code> 和 <code v-pre>password_confirmation</code> 字段。电子邮件/用户名字段的名称应与你的应用程序 <code v-pre>fortify</code> 配置文件中定义的 <code v-pre>username</code> 配置值匹配。</p>
<p>如果注册尝试成功，Fortify 将重定向用户到通过你的应用程序 <code v-pre>fortify</code> 配置文件中的 <code v-pre>home</code> 配置选项配置的 URI 。如果请求是一个 XHR 请求，则将返回一个 201 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回注册屏幕，并且验证错误将通过共享的 <code v-pre>$errors</code> Blade 模板变量提供给你。或者，在 XHR 请求的情况下，验证错误将在 422 HTTP 响应中返回。</p>
<h3 id="自定义注册" tabindex="-1"><a class="header-anchor" href="#自定义注册"><span>自定义注册</span></a></h3>
<p>用户验证和创建过程可以通过修改安装 Laravel Fortify 时生成的<code v-pre>App\Actions\Fortify\CreateNewUser</code> 动作来进行自定义。</p>
<h2 id="密码重置" tabindex="-1"><a class="header-anchor" href="#密码重置"><span>密码重置</span></a></h2>
<h3 id="请求密码重置链接" tabindex="-1"><a class="header-anchor" href="#请求密码重置链接"><span>请求密码重置链接</span></a></h3>
<p>要开始实现我们应用程序的密码重置功能，我们需要指示 Fortify 如何返回我们的「forgot password」视图。请记併，Fortify 是一个无头身份验证库。如果你想要一个已经为你完成的 Laravel 身份验证功能的前端实现，你应该使用一个<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">应用程序起始工具包</a>。</p>
<p>所有 Fortify 的视图渲染逻辑都可以使用 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常情况下，你应该从你的应用程序<code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">requestPasswordResetLinkView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.forgot-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 会负责定义返回此视图的 <code v-pre>/forgot-password</code> 路由。你的 <code v-pre>forgot-password</code>模板应包含一个表单，该表单会向 <code v-pre>/forgot-password</code> 端点发起POST请求。</p>
<p><code v-pre>/forgot-password</code> 端点需要一个字符串 <code v-pre>email</code> 字段。这个字段/数据库列的名称应与你的应用程序 <code v-pre>fortify</code> 配置文件中定义的 <code v-pre>email</code> 配置值匹配。</p>
<h4 id="处理密码重置链接请求响应" tabindex="-1"><a class="header-anchor" href="#处理密码重置链接请求响应"><span>处理密码重置链接请求响应</span></a></h4>
<p>如果密码重置链接请求成功，Fortify 将重定向用户回到 <code v-pre>/forgot-password</code> 端点，并向用户发送一封包含安全链接的电子邮件，用户可以使用该链接重置密码。如果请求是一个 XHR 请求，则将返回一个 200 HTTP 响应。</p>
<p>在成功请求后重定向回 <code v-pre>/forgot-password</code> 端点后，<code v-pre>status</code> 会话变量可用于显示密码重置链接请求尝试的状态。</p>
<p><code v-pre>$status</code> 会话变量的值将匹配你的应用程序 <code v-pre>passwords</code> <a href="https://learnku.com/docs/laravel/11.x/localization" target="_blank" rel="noopener noreferrer">语言文件</a>中定义的翻译字符串之一。如果你想自定义此值并且尚未发布Laravel的语言文件，你可以通过 <code v-pre>lang:publish</code> Artisan命令来执行此操作：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line">@if (session('status'))</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mb-4 font-medium text-sm text-green-600<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        {{ session('status') }}</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果请求不成功，用户将被重定向回请求密码重置链接屏幕，并且验证错误将通过共享的 <code v-pre>$errors</code><a href="https://learnku.com/docs/laravel/11.x/validation#quick-displaying-the-validation-errors" target="_blank" rel="noopener noreferrer">Blade模板变量</a> 提供给你。或者，在 XHR 请求的情况下，验证错误将以 422 HTTP 响应返回。</p>
<h3 id="重置密码" tabindex="-1"><a class="header-anchor" href="#重置密码"><span>重置密码</span></a></h3>
<p>要完成我们应用程序的密码重置功能的实现，我们需要指示Fortify如何返回我们的「重置密码」视图。</p>
<p>所有Fortify的视图渲染逻辑都可以使用 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常情况下，你应该从你的应用程序<code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">resetPasswordView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.reset-password'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'request'</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify会负责定义显示此视图的路由。你的 <code v-pre>reset-password</code> 模板应包含一个表单，该表单会向 <code v-pre>/reset-password</code> 发起 POST 请求。</p>
<p><code v-pre>/reset-password</code> 端点需要一个字符串 <code v-pre>email</code> 字段，一个 <code v-pre>password</code> 字段，一个 <code v-pre>password_confirmation</code> 字段，以及一个名为 <code v-pre>token</code> 的隐藏字段，其中包含<code v-pre>request()-&gt;route('token')</code> 的值。 「email」字段/数据库列的名称应与你的应用程序 <code v-pre>fortify</code> 配置文件中定义的 <code v-pre>email</code> 配置值匹配。</p>
<h4 id="处理密码重置响应" tabindex="-1"><a class="header-anchor" href="#处理密码重置响应"><span>处理密码重置响应</span></a></h4>
<p>如果密码重置请求成功，Fortify将重定向回 <code v-pre>/login</code> 路由，以便用户可以使用新密码登录。此外，将设置一个 <code v-pre>status</code> 会话变量，以便在登录屏幕上显示重置的成功状态：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@if (session('status'))</span>
<span class="line">    &lt;div class=&quot;mb-4 font-medium text-sm text-green-600&quot;&gt;</span>
<span class="line">        {{ session('status') }}</span>
<span class="line">    &lt;/div&gt;</span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果请求是一个 XHR 请求，将返回一个 200 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回重置密码屏幕，并且验证错误将通过共享的<code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/11.x/validation#quick-displaying-the-validation-errors" target="_blank" rel="noopener noreferrer">Blade 模板变量</a>提供给你。或者，在 XHR 请求的情况下，验证错误将以 422 HTTP 响应返回。</p>
<h3 id="自定义密码重置" tabindex="-1"><a class="header-anchor" href="#自定义密码重置"><span>自定义密码重置</span></a></h3>
<p>密码重置流程可以通过修改在安装 Laravel Fortify 时生成的<code v-pre>App\Actions\ResetUserPassword</code>操作来进行自定义。</p>
<h2 id="电子邮件验证" tabindex="-1"><a class="header-anchor" href="#电子邮件验证"><span>电子邮件验证</span></a></h2>
<p>注册后，你可能希望用户在继续访问你的应用程序之前验证他们的电子邮件地址。要开始，请确保在你的 <code v-pre>fortify</code> 配置文件的 <code v-pre>features</code> 数组中启用<code v-pre>emailVerification</code> 功能。接下来，你应该确保你的 <code v-pre>App\Models\User</code> 类实现了<code v-pre>Illuminate\Contracts\Auth\MustVerifyEmail</code> 接口。</p>
<p>完成这两个设置步骤后，新注册用户将收到一封提示他们验证电子邮件地址所有权的电子邮件。然而，我们需要告诉 Fortify 如何显示电子邮件验证屏幕，提示用户需要点击电子邮件中的验证链接。</p>
<p>所有 Fortify 视图的渲染逻辑都可以使用 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常情况下，你应该从你的应用程序<code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">verifyEmailView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.verify-email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 会负责定义路由，当用户被 Laravel 内置的 <code v-pre>verified</code> 中间件重定向到 <code v-pre>/email/verify</code> 端点时，显示此视图。</p>
<p>你的 <code v-pre>verify-email</code> 模板应包含一条信息性消息，指示用户点击发送到他们电子邮件地址的电子邮件验证链接。</p>
<h4 id="重新发送电子邮件验证链接" tabindex="-1"><a class="header-anchor" href="#重新发送电子邮件验证链接"><span>重新发送电子邮件验证链接</span></a></h4>
<p>如果你希望，在你的应用程序的 <code v-pre>verify-email</code> 模板中添加一个按钮，触发一个向 <code v-pre>/email/verification-notification</code> 端点的 POST 请求。当此端点收到请求时，将向用户发送一个新的验证电子邮件链接，允许用户获取新的验证链接，如果之前的链接被意外删除或丢失。</p>
<p>如果重新发送验证链接电子邮件的请求成功，Fortify 将重定向用户回 <code v-pre>/email/verify</code> 端点，并设置一个 <code v-pre>status</code> 会话变量，允许你向用户显示一条信息性消息，告知操作成功。如果请求是一个XHR请求，则将返回一个202 HTTP响应：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@if (session('status') == 'verification-link-sent')</span>
<span class="line">    &lt;div class=&quot;mb-4 font-medium text-sm text-green-600&quot;&gt;</span>
<span class="line">        一个新的电子邮件验证链接已发送到您的邮箱！</span>
<span class="line">    &lt;/div&gt;</span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="保护路由" tabindex="-1"><a class="header-anchor" href="#保护路由"><span>保护路由</span></a></h3>
<p>要指定一个路由或一组路由要求用户已验证他们的电子邮件地址，你应该将 Laravel 内置的 <code v-pre>verified</code> 中间件附加到路由上。<code v-pre>verified</code> 中间件别名由 Laravel 自动注册，并作为 <code v-pre>Illuminate\Routing\Middleware\ValidateSignature</code> 中间件的别名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'verified'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="密码确认" tabindex="-1"><a class="header-anchor" href="#密码确认"><span>密码确认</span></a></h2>
<p>在构建你的应用程序时，你可能偶尔会有需要用户在执行操作之前确认他们的密码的情况。通常情况下，这些路由受 Laravel 内置的 <code v-pre>password.confirm</code> 中间件保护。</p>
<p>要开始实现密码确认功能，我们需要指示 Fortify 如何返回我们应用程序的“密码确认”视图。请记住，Fortify 是一个无头身份验证库。如果你想要一个已为你完成的 Laravel 身份验证功能的前端实现，你应该使用一个<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">应用程序起始工具包</a>。</p>
<p>所有 Fortify 的视图渲染逻辑都可以使用<code v-pre>Laravel\Fortify\Fortify</code>类提供的适当方法进行自定义。通常情况下，你应该从你的应用程序<code v-pre>App\Providers\FortifyServiceProvider</code>类的<code v-pre>boot</code>方法中调用这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">confirmPasswordView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.confirm-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 会负责定义返回此视图的 <code v-pre>/user/confirm-password</code> 路由。你的 <code v-pre>confirm-password</code> 模板应包含一个表单，该表单会向 <code v-pre>/user/confirm-password</code> 端点发起POST请求。<code v-pre>/user/confirm-password</code> 端点期望一个包含用户当前密码的<code v-pre>password</code>字段。</p>
<p>如果密码与用户的当前密码匹配，Fortify 将重定向用户到他们试图访问的路由。如果请求是一个 XHR 请求，则将返回一个 201 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回确认密码屏幕，并且验证错误将通过共享的<code v-pre>$errors</code> Blade 模板变量提供给你。或者，在 XHR 请求的情况下，验证错误将以 422 HTTP 响应返回。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/fortifymd/16718" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/fo...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/fortifymd/16718" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/fo...</a></p>
</blockquote>
</div></template>


